<?php
/**
 * Created by PhpStorm.
 * User: serafina
 * Date: 2015/9/23
 * Time: 09:50
 */


function mobile_service_experts_work_list($parameters){
    global $user;
    $query = db_select('experts_work', 'V');
    $datestr = '';
    if (!empty($form_state['values']['year'])) {//查询条件
        if ($form_state['values']['year']) {
            $datestr .= $form_state['values']['year'] . '-';
        }
    }
    if (!empty($form_state['values']['month'])) {//查询条件
        if ($month = $form_state['values']['month']) {
            if ($month < 10)
                $datestr .= '0' . $form_state['values']['month'];
            else
                $datestr .= $form_state['values']['month'];
        }
    }
    if(!user_access('view all experts_work') && user_access('view own experts_work')){
        $query->condition('V.author_uid',$user->uid, '=');
    }
    if ($datestr) {
        $query->condition('V.work_date', db_like($datestr) . '%', 'LIKE');
    }
    if (!empty($form_state['values']['title_fuzzy'])) {//查询条件
        $query->condition('V.title', '%'.db_like($form_state['values']['title_fuzzy']) . '%', 'LIKE');
    }
    if (!empty($form_state['values']['content_fuzzy'])) {//查询条件
        $query->condition('V.content', '%'.db_like($form_state['values']['content_fuzzy']) . '%', 'LIKE');
    }
    if (!empty($form_state['values']['attendees_fuzzy'])) {//查询条件
        $query->condition('V.attendees', '%'.db_like($form_state['values']['attendees_fuzzy']) . '%', 'LIKE');
    }
    if(!empty($form_state['values']['author_uid'])){
        $query->condition('V.author_uid',$form_state['values']['author_uid'], '=');
    }
    $query->fields('V');

    $query->orderBy('V.experts_work_id', 'DESC');
    if (!empty($parameters['cursor']) && !empty($parameters['comparator'])) { //游标翻页
        $query->condition('V.experts_work_id', $parameters['cursor'], $parameters['comparator']);
    }
    $query->range(0,10);
    
    $results = $query->execute();
    $result_list = [];
    if(!empty($results)){
        foreach ($results as $result) {
            $result_list[] = $result;
        }
    }
    return  ["data"=>$result_list];
}
function mobile_service_experts_work_add($parameters){
    global $user;
    $module_name="experts_work";
    $id = !empty($parameters['experts_work_id'])?$parameters['experts_work_id']:'';
    if (empty($id)) { //add new brand_sales
        if(!user_access('add '.$module_name)){
            return ['option'=>0,'errorNum'=>3];
        }
        $nid = db_insert('experts_work') // Table name no longer needs {}
        ->fields(array(
            'title' => $parameters['title'],
            'experts_id' => $parameters['experts_id'],
            'content' => $parameters['content'],
            'attendees' => $parameters['attendees'],
            'work_date' => $parameters['work_date'],
            'address' => $parameters['address'],
            'author_uid' => $user->uid,
            'timestamp' => time(),))
            ->execute();
        $result_option = (object)["option" => 1];
        return $result_option;
    } else {
        if(!mobile_user_edit_access($module_name,$module_name.'_id',$id,'edit own '.$module_name,'edit all '.$module_name)){
            return ['option'=>0,'errorNum'=>3];
        }
        $nid = db_update('experts_work') // edit exsting store. todo: update only modified fields
        ->fields(array(
            'title' => $parameters['title'],
            'experts_id' => $parameters['experts_id'],
            'content' => $parameters['content'],
            'attendees' => $parameters['attendees'],
            'work_date' => $parameters['work_date'],
            'address' => $parameters['address'],
            'timestamp' => time(),))
            -> condition('experts_work_id',$id, '=')
            ->execute();
        if($nid > 0){
            $result_option = (object)["option" => 1];
            return $result_option;
        }
        $result_option = (object)["option" => 0];
        return $result_option;
    }
}









